#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 1e5 + 5;

int n, m;
int v[N];
int f[N], lc[N], rc[N], dis[N];
void init() {
  rep(i, 1, n) f[i] = i;
  rep(i, 1, n) lc[i] = rc[i] = dis[i] = 0;
  dis[0] = -1;
}
int find(int x) { return f[x] == x ? x : f[x] = find(f[x]); }
int merge(int x, int y) {
  if (!x || !y) return x + y;
  if (v[x] == v[y] ? x > y : v[x] < v[y]) swap(x, y);
  rc[x] = merge(rc[x], y);
  if (dis[lc[x]] < dis[rc[x]]) swap(lc[x], rc[x]);
  dis[x] = dis[rc[x]] + 1;
  return x;
}
int fight(int x) {
  v[x] /= 2;
  int y = lc[x], z = rc[x];
  lc[x] = rc[x] = 0;
  f[x] = f[y] = f[z] = merge(x, merge(y, z));
  return f[x];
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  while (cin >> n) {
    rep(i, 1, n) cin >> v[i];
    init();
    cin >> m;
    while (m--) {
      int x, y;
      cin >> x >> y;
      x = find(x), y = find(y);
      if (x == y) {
        cout << -1 << endl;
      } else {
        x = fight(x), y = fight(y);
        f[x] = f[y] = merge(x, y);
        cout << v[f[x]] << endl;
      }
    }
  }
  return 0;
}